package cn.com.simple.learning.designpattern.create.builder;

/**
 * @author Jianxing.Jia
 * @date 2022/2/7
 * @description 用户对象
 */
public class User {

    private String id;

    private int age;

    private String realName;

    private String nickname;

    // 构造方法私有
    private User(String id, int age, String realName, String nickname) {
        this.id = id;
        this.age = age;
        this.realName = realName;
        this.nickname = nickname;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public static UserBuilder builder() {
        return new UserBuilder();
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", age=" + age +
                ", realName='" + realName + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }

    /**
     * 用户构建对象 拥有跟用户对象一样的属性
     */
    public static class UserBuilder {

        private String id;

        private int age;

        private String realName;

        private String nickname;

        public UserBuilder id(String id) {
            this.id = id;
            return this;
        }

        public UserBuilder age(int age) {
            this.age = age;
            return this;
        }

        public UserBuilder realName(String realName) {
            this.realName = realName;
            return this;
        }

        public UserBuilder nickname(String nickname) {
            this.nickname = nickname;
            return this;
        }

        public User build() {
            return new User(id, age, realName, nickname);
        }
    }

    /**
     * 测试
     */
    public static void main(String[] args) {
        User user = User.builder().age(10).id("1").nickname("zs").realName("zs").build();
        System.out.println(user);
    }
}
